OpenCV 101


In [1]:
# this line imports several useful function (including imshow)
%pylab inline


Populating the interactive namespace from numpy and matplotlib

In [2]:
# import OpenCV
import cv, cv2

Load a video


In [3]:
episode1 = '/vol/corpora4/tvseries/tvd/GameOfThrones/dvd/rip/video/GameOfThrones.Season01.Episode01.mkv'
capture = cv2.VideoCapture(episode1)

Read frames sequentially


In [4]:
for i in range(1700):
    _, bgr = capture.read()

Display frames


In [5]:
print "Behind the wall..."
imshow(bgr);


Behind the wall...

Change colorspace


In [6]:
print "... it's actually cold!"
rgb = cv2.cvtColor(bgr, cv.CV_BGR2RGB)
imshow(rgb);


... it's actually cold!

Get current position in video


In [7]:
frameNumber = int(capture.get(cv.CV_CAP_PROP_POS_FRAMES))
currentTime = float(capture.get(cv.CV_CAP_PROP_POS_MSEC)) / 1000
print "Frame = #%d / Time = %.3fs" % (frameNumber, currentTime)


Frame = #1700 / Time = 68.000s